package com.loadmore.baselibrary.widget;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.loadmore.baselibrary.util.Util;

/**
 * Created by sunleilei on 2017/3/10
 */

public class CustomWindow {
    private boolean isShow;
    private boolean moveable;
    private Context context;
    private LayoutInflater inflater;
    private FrameLayout rootView;
    private WindowManager windowManager;
    private WindowManager.LayoutParams params;
    public CustomWindow(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        init();
    }

    private void init() {
        windowManager = Util.getWindowManager(context);
        rootView = new FrameLayout(context);
        rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        params = new WindowManager.LayoutParams();
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.format = PixelFormat.TRANSLUCENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.type = WindowManager.LayoutParams.TYPE_PHONE;
        params.x = 0;
        params.y = 0;
        params.gravity = Gravity.LEFT|Gravity.TOP;
        rootView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                moveable = true;
                return true;
            }
        });
        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!moveable)
                    return false;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        updatePosition(event.getRawX()-rootView.getMeasuredWidth()/2,event.getRawY()-rootView.getMeasuredHeight()/2- Util.getStatusBarHeight(context));
                        break;
                    case MotionEvent.ACTION_UP:
                        moveable = false;
                        break;
                }
                return true;
            }
        });
    }

    public View setUpView(int id) {
        View view = inflater.inflate(id,null);
        rootView.addView(view);
        return view;
    }

    public void setUpView(View view) {
        rootView.addView(view);
    }

    public void updatePosition(float x, float y) {
        if (!isShow)
            return;
        params.x = (int) x;
        params.y = (int) y;
        windowManager.updateViewLayout(rootView,params);
    }

    public void show() {
        isShow = true;
        windowManager.addView(rootView,params);
    }

    public void show(int x,int y) {
        isShow = true;
        params.x = x;
        params.y = y;
        windowManager.addView(rootView,params);
    }
    public void hide() {
        windowManager.removeView(rootView);
        isShow = false;
    }
}
